<?php
// +----------------------------------------------------------------------
// | 小牛Admin
// +----------------------------------------------------------------------
// | Website: www.xnadmin.cn
// +----------------------------------------------------------------------
// | Author: dav <85168163@qq.com>
// +----------------------------------------------------------------------
namespace app\api\model;

use think\Model;

class Token extends Model
{
    public static function getToken($webInfo){
        $appid = $webInfo['xcx_appid'];
        $appsecret = $webInfo['xcx_secret'];

        $userInfo   = self::where(['xcx_appid' => $appid])->find();
        if(empty($userInfo) || $userInfo['time_out'] < date('Y-m-d H:i:s', time() + 300)){
            $access_token = self::getRemoteToken($webInfo);
        }else{
            $access_token = $userInfo['access_token'];
        }

        return $access_token;
    }


    public static function getRemoteToken($webInfo){
        $appid = $webInfo['xcx_appid'];
        $appsecret = $webInfo['xcx_secret'];

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
        $weixin = file_get_contents($url);
        $jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
        $array = get_object_vars($jsondecode);//转换成数组

        $access_token = $array['access_token'];//输出token
        $time_out = date("Y-m-d H:i:s", $array['expires_in'] + time());//输出token

        $userInfo   = self::where(['xcx_appid' => $appid])->find();
        if($userInfo){
            self::where(['xcx_appid' => $appid])->update([
                'access_token' => $access_token,
                'time_out' => $time_out,
            ]);
        }else{
            self::insert([
                'xcx_appid' => $appid,
                'access_token' => $access_token,
                'time_out' => $time_out,
            ]);
        }
        return $access_token;
    }


    public static function msSecCheck($openid, $scene, $msg){
        $webInfo = xn_cfg('base');
        $token = self::getToken($webInfo);
        $ewmurl = "https://api.weixin.qq.com/wxa/msg_sec_check?access_token=".$token;
        $data = [
              "openid"=> $openid,
              "scene"=> $scene,
              "version"=> 2,
              "content"=>$msg
            ];

        $datas = json_encode($data, JSON_UNESCAPED_UNICODE);
        $result = _Postrequest($ewmurl, $datas);
        $data=json_decode($result, true);
        //dump($data, $datas,$ewmurl);exit();
        return $data['result']['label'] == 100;
    }


    public static function msSecCheckImage($openid, $scene, $msg){
        $webInfo = xn_cfg('base');
        $token = self::getToken($webInfo);
        $ewmurl = "https://api.weixin.qq.com/wxa/media_check_async?access_token=".$token;
        $data = [
            "openid"=> $openid,
            "scene"=> $scene,
            "version"=> 2,
            "media_type" => 1,
            "media_url"=>$msg
        ];

        $datas = json_encode($data, JSON_UNESCAPED_UNICODE);
        $result = _Postrequest($ewmurl, $datas);
        $data=json_decode($result, true);
        //dump($data, $datas,$ewmurl);exit();
        return $data['result']['label'] == 100;
    }


}
